home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / AOCE / DragBusinessCard / GetAttribute.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-11  |  4.9 KB  |  182 lines  |  [TEXT/MPS ]

  1. /*
  2.  * GetAttribute.c
  3.  */
  4.  
  5. #include "SimpleDrag.h"
  6.  
  7. #define     kGetAttrBufferSize        (4096)    /* Working data buffer    */
  8.  
  9. static pascal Boolean myForEachLookupRecordID (long clientData, const RecordID *recordID);
  10. static pascal Boolean myForEachAttrValue (long clientData, const Attribute *attribute);
  11. static pascal Boolean myForEachAttrTypeLookup (long clientData,
  12.     const AttributeType *attrType, AccessMask myAttrAccMask);
  13.  
  14.  
  15.  
  16.  
  17. /*********************************************************************************
  18.  * OpenPersonalCatalog
  19.  * If the current selection is an alias, open the personal address catalog that is
  20.  * related to that alias. The personal catalog is opened for reading. Note:
  21.  * this function returns kOCEMiscError if the rli is *not* an alias. This
  22.  * is not really an error. Other error returns indicate a failed attempt to
  23.  * open the catalog.
  24.  *********************************************************************************/
  25. OSErr
  26. OpenPersonalCatalog(
  27.                     PackedRLIPtr        rli,
  28.                     short                *dsRefNumPtr
  29.                     )
  30. {
  31. OSErr                status;
  32. AliasPtr            aliasPtr;
  33. AliasHandle            aliasHandle;
  34. Boolean                wasChanged;
  35. FSSpec                theFSSpec;
  36. DirParamBlock        dirParamBlock;
  37. #define OPEN (dirParamBlock.openPersonalDirectoryPB)
  38.  
  39.         aliasHandle = NULL;
  40.         *dsRefNumPtr = 0;
  41.         status = noErr;
  42.         if (OCEValidPackedRLI(rli) == false
  43.              || (aliasPtr = OCEExtractAlias(rli)) == NULL)
  44.                  status = kOCEMiscError;                /* Not an alias    */
  45.  
  46.         if (status == noErr)
  47.         {
  48.             status = PtrToHand((Ptr) aliasPtr, 
  49.                                 (Handle *) &aliasHandle,
  50.                                 aliasPtr->aliasSize);
  51.         }
  52.         
  53.         if (status == noErr)
  54.             status = ResolveAlias(NULL, aliasHandle, &theFSSpec, &wasChanged);
  55.             
  56.         if (aliasHandle != NULL)
  57.             DisposeHandle((Handle) aliasHandle);
  58.  
  59.         if (status == noErr)
  60.         {
  61.             OPEN.fsSpec = &theFSSpec;
  62.             OPEN.accessRequested = fsRdPerm;
  63.             status = DirOpenPersonalDirectory(&dirParamBlock);
  64.             if (status == noErr)
  65.                 *dsRefNumPtr = OPEN.dsRefNum;
  66.         }
  67.  
  68.         return (status);
  69. #undef OPEN
  70. }
  71.  
  72.  
  73.  
  74.  
  75.  
  76. /*****************************************************************************
  77.  * GetAttributeFromRID
  78.  * Given a record id, this routine performs a DirLookUpGet/DirLookUpParse
  79.  * combination to retrieve the attributes of the given type from the record.
  80.  * 
  81.  *****************************************************************************/
  82.  
  83. OSErr GetAttributeFromRID ( RecordIDPtr             rid,
  84.                             short                     dsRefNum,
  85.                             const AttributeTypePtr     attributeType,
  86.                             AttributePtr             attributePtr,
  87.                             AuthIdentity             identity)
  88. {
  89.     OSErr                err;
  90.     RecordIDPtr            recordList[1];
  91.     AttributeTypePtr    attTypeList[1];
  92.     DirParamBlock        dirParamBlock;
  93.     Ptr                    buffer;
  94. #define     GET            (dirParamBlock.lookupGetPB)
  95. #define     PARSE        (dirParamBlock.lookupParsePB)
  96.  
  97.  
  98.         recordList[0] = rid;
  99.         attTypeList[0] = attributeType;
  100.     
  101.         buffer = NewPtr(kGetAttrBufferSize);
  102.         if (buffer == NULL)
  103.             err = MemError();
  104.         else 
  105.         {
  106.             GET.serverHint.aNet = 0;
  107.             GET.serverHint.aNode = 0;
  108.             GET.serverHint.aSocket = 0;
  109.             GET.dsRefNum = dsRefNum;
  110.             GET.identity = identity;
  111.             GET.clientData = 0L;
  112.             GET.aRecordList = &recordList;
  113.             GET.attrTypeList = &attTypeList;
  114.             GET.recordIDCount = 1;
  115.             GET.attrTypeCount = 1;
  116.             GET.includeStartingPoint = true;
  117.             GET.getBuffer = buffer;
  118.             GET.getBufferSize = kGetAttrBufferSize;
  119.             GET.startingRecordIndex = 1;
  120.             GET.startingAttrTypeIndex = 1;
  121.             GET.startingAttribute.cid.source = 0L;
  122.             GET.startingAttribute.cid.seq = 0L;
  123.             err = DirLookupGet (&dirParamBlock, false);
  124.  
  125.             if (err == noErr || err == kOCEMoreData)
  126.             {
  127.     
  128.                 PARSE.clientData = (long) attributePtr;
  129.                 PARSE.eachRecordID = myForEachLookupRecordID;
  130.                 PARSE.eachAttrType = myForEachAttrTypeLookup;
  131.                 PARSE.eachAttrValue = myForEachAttrValue;
  132.                 
  133.                 err = DirLookupParse (&dirParamBlock, false);
  134.             }
  135.         
  136.             DisposePtr (buffer);
  137.         }
  138.         
  139.     return (err);
  140. #undef GET
  141. #undef PARSE
  142.  
  143. }
  144.  
  145. static pascal Boolean myForEachLookupRecordID ( long            clientData,
  146.                                                 const RecordID     *recordID)
  147. {
  148. #pragma unused (clientData,recordID)
  149.  
  150.     return (false);
  151. }
  152.  
  153. static pascal Boolean myForEachAttrTypeLookup ( long                clientData,
  154.                                                 const AttributeType *attrType, 
  155.                                                 AccessMask             myAttrAccMask)
  156. {
  157. #pragma unused (clientData,attrType,myAttrAccMask)
  158.  
  159.     return (false);
  160. }
  161.  
  162. static pascal Boolean myForEachAttrValue (    long             clientData,
  163.                                             const Attribute *attribute)
  164. {
  165.     AttributePtr    theAttribute;
  166.  
  167.         theAttribute = (AttributePtr) clientData;
  168.         memcpy (theAttribute, (Ptr) attribute, sizeof (Attribute));
  169.  
  170.         if (((*theAttribute).value.bytes = NewPtr ((*attribute).value.dataLength)) != NULL)
  171.             memcpy ((*theAttribute).value.bytes, (*attribute).value.bytes,
  172.                     (*attribute).value.dataLength);
  173.         else
  174.         {
  175.                 /* couldn't allocate memory for the attribute value, so we punt
  176.                     and zero out the attribute value fields */
  177.             (*theAttribute).value.dataLength = 0;        /* 0 length */
  178.             (*theAttribute).value.bytes = NULL;        /* null ptr to attr. value data */
  179.         }
  180.     
  181.     return (true);
  182. }